<?php


namespace app\admin\traits;

use EasyAdmin\annotation\NodeAnotation;
use EasyAdmin\tool\CommonTool;
use jianyan\excel\Excel;
use think\facade\Db;

/**
 * 后台拼接html复用
 * Trait Curd
 * @package app\admin\traits
 */
trait BuildHtml
{

    function buildConfigs($configs)
    {
        $html = "";
        foreach ($configs as $config) {
            $html .= '<div class="layui-form-item">';
            $html .= '<label class="layui-form-label">' . $config['title'] . '</label>';
            switch ($config['type']) {
                case 'text':
                    $html .= $this->buildConfigText($config);
                    break;
                case 'image':
                    $html .= $this->buildConfigImage($config);
                    break;
                case 'images':
                    $html .= $this->buildConfigImage($config, true);
                    break;
                case 'editor':
                    $html .= $this->buildConfigEditor($config);
                    break;
                default :
                    $html .= $this->buildConfigText($config);
            }
            $html .= '</div>';
        }
        return $html;
    }

    function buildConfigImage($config, $mul = true)
    {
        $html = '<div class="layui-input-block layuimini-upload">';
        $html .= '<input name="' . $config['name'] . '" class="layui-input layui-col-xs6" lay-verify="required" placeholder="请上传' . $config['title'] . '" value="' . $config['value'] . '">';
        $html .= '<div class="layuimini-upload-btn">';
        $html .= '<span><a class="layui-btn" data-upload="' . $config['name'] . '" data-upload-number="' . ($mul ? 'more' : 'one') . '" data-upload-exts="ico|png|jpg|jpeg"><i class="fa fa-upload"></i> 上传</a></span>';
        $html .= '<span><a class="layui-btn layui-btn-normal" id="select_' . $config['name'] . '" data-upload-select="' . $config['name'] . '" data-upload-number="' . ($mul ? 'more' : 'one') . '"><i class="fa fa-list"></i> 选择</a></span>';
        $html .= '</div><ul id="bing-' . $config['name'] . '" class="layui-input-block layuimini-upload-show">';
//        <ul id="bing-app_banner_image" class="layui-input-block layuimini-upload-show">
        $img_urls = explode('|', $config['value']);
        foreach ($img_urls as $url) {
            $html .= '<li><a><img src="' . $url . '" data-image="" onerror="this.src=\'' . $url . '\';this.onerror=null"></a>';
            $html .= '<small class="uploads-delete-tip bg-red badge" data-upload-delete="' . $config['name'] . '" data-upload-url="' . $url . '" data-upload-sign="|">×</small></li>';
        }
        $html .= '</ul></div>';
        return $html;
    }

    function buildConfigText($config)
    {
        $html = '<div class="layui-input-block">';
        $html .= ' <input type="text" name="' . $config['name'] . '" class="layui-input is_long' . $config['is_long_rule'] . '" lay-verify="required"
                                placeholder="' . ($config['placeholder'] ?? '请输入规则') . '" value="' . $config['value'] . '">';
        $html .= '<tip>' . $config['remark'] . ($config['example'] ?: '<span class="color-red">【' . $config['example'] . '】</span>') . '</tip>';
        $html .= '</div>';
        return $html;
    }

    function buildConfigEditor($config)
    {
        return
            '<div class="layui-input-block">
                <textarea name="' . $config['name'] . '" rows="20" class="layui-textarea editor" placeholder="请编辑' . $config['title'] . '" >' . $config['value'] . '</textarea>
            </div>';
    }


}
